﻿/* This code has two Gaussian pulses moving in opposite directions on a string meeting at the center. 
It illustrates the linearity of string displacements.  It also shows how to understand the reflection 
of a pulse at the end of a string, both for a fixed end and one free to move in a perpendicular direction, 
since the string motion is identical to that of one-half of the full (no central boundary) string 
for equal but oppositely signed pulses corresponding to the fixed end -- the central point never moves, etc.

See below for the adjustable parameters alph, alph_compnt and ampl_2. */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	public class ReflectedPacket_3 extends Sprite {


		public var center_init: Number;
		public var center: Number;
		public var spread: Number;
		public var color: uint;
		private var wave_x: Number;
		private var delta_x: Number;
		private var wave_y;
		private var wave_1_y;
		private var wave_2_y;
		private var reflector: Number;
		private var speed: Number = 4;
		private var i: uint = 0;
		private var alph: Number;
		private var alph_compnt: Number;
		private var ampl_2: Number;


		public function ReflectedPacket_3(center:Number = 100, spread:Number = 20, color:uint=0xff0000) {
			this.center_init = center_init;
			this.center = center;
			this.spread = spread;
			this.color = color;
			this.alph = alph;
			this.alph_compnt = alph_compnt;
			this.ampl_2 = ampl_2;

			init();
		}
		public function init():void {

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			spread = 30;
			center_init = 0.15*stage.stageWidth;
			center = center_init;
			wave_x = 10;
			delta_x = 0.5;
			reflector = 0.5*stage.stageWidth;
			
			/*This "alph" = 1 gives two pulses meeting on a string, alph = 0 gives
			 one pulse going towards a fixed end in the middle.*/
			alph = 0;
			
			/*This alph_compt is the alpha values of the (red and green) separate pulses that add 
			to give the full string displacememt. If it's set to zero, you just see the actual string. */
			alph_compnt = 0;
			
			/* The pulse coming in from the left has amplitude 50. This ampl_2 is the (signed) amplitude of the pulse 
			coming in from the right. To illustrate the boundary conditions, it must be 50 or -50, but can be different for
			the full string, of course, to illustrate linearity of addition. */
			ampl_2 = -50;

			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		public function onEnterFrame(event:Event):void {

			if (center < stage.stageWidth - center_init) {
				graphics.clear();
				center += speed;


				graphics.lineStyle(3, 0x000000, 1 - alph);
				graphics.moveTo(reflector, 190);
				graphics.lineTo(reflector, 210);

				graphics.lineStyle(1, 0x000000, 0);
				graphics.moveTo(reflector, 200);
				graphics.lineTo(0, 200);


				for (i = 0; i < (2*reflector/delta_x); i++) {
					wave_x =  i*delta_x;
					wave_y = 200 + wave_1_y + wave_2_y;

					wave_1_y = -(50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread))));
					wave_2_y = -(ampl_2*Math.exp(-((wave_x - (2*reflector - center))*(wave_x - (2*reflector - center))/(spread*spread))));

					if (i < (reflector/delta_x)) {
						graphics.lineStyle(.1, 0x00ff00, alph_compnt);
					} else {
						graphics.lineStyle(.1, 0x00ff00, alph*alph_compnt);
					}
					graphics.lineTo(wave_x, 200 + wave_1_y);
				}
				graphics.moveTo(0,200);

				for (i = 0; i < (2*reflector/delta_x); i++) {
					wave_x =  i*delta_x;
					wave_y =  wave_1_y + wave_2_y;

					wave_1_y = -(50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread))));
					wave_2_y = -(ampl_2*Math.exp(-((wave_x - (2*reflector - center))*(wave_x - (2*reflector - center))/(spread*spread))));

					if (i < (reflector/delta_x)) {
						graphics.lineStyle(.5, 0xff0000, alph_compnt);
					} else {
						graphics.lineStyle(.5, 0xff0000, alph*alph_compnt);
					}
					graphics.lineTo(wave_x, 200 + wave_2_y);
				}
				graphics.moveTo(0,200);

				for (i = 0; i < (2*reflector/delta_x); i++) {
					wave_x =  i*delta_x;
					wave_y =  wave_1_y + wave_2_y;

					wave_1_y = -(50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread))));
					wave_2_y = -(ampl_2*Math.exp(-((wave_x - (2*reflector - center))*(wave_x - (2*reflector - center))/(spread*spread))));

					if (i < (reflector/delta_x)) {
						graphics.lineStyle(1, 0x000000, 1);
					} else {
						graphics.lineStyle(1, 0x000000, alph);
					}
					graphics.lineTo(wave_x, 200 + wave_y);
				}
				graphics.moveTo(0,200);

			}
		}
	}
}